package org.ytor.core.querygen.support;

import org.springframework.stereotype.Component;
import org.ytor.common.util.Strs;
import org.ytor.core.querygen.AbsWhere;
import org.ytor.core.querygen.token.Token;
import org.ytor.core.sqlflow.builder.support.WhereBuilder;

/**
 * created by yangtong on 2025/8/11 14:16:56
 * <br/>
 * 等值匹配，age=1，优先级最高
 */
@Component
public class EqWhere extends AbsWhere {

    @Override
    public Boolean isMatch(Token token) {
        String k = token.getKey();
        String v = token.getValue();
        if (Strs.isEmpty(k) || Strs.isEmpty(v)) return false;
        if (k.endsWith("_gt") || k.endsWith("_ge") || k.endsWith("_lt") || k.endsWith("_le")
                || k.endsWith("_in") || k.endsWith("_or")) return false;
        return !v.startsWith("*") || !v.endsWith("*");
    }

    @Override
    protected WhereBuilder positive(WhereBuilder where, Token token) {
        return (WhereBuilder) where.eq(Strs.toUnderline(token.getKey()), token.getValue());
    }

    @Override
    protected WhereBuilder negate(WhereBuilder where, Token token) {
        // 没有 notEq，直接用 expr 拼： (col != ?)
        return (WhereBuilder) where.expr(Strs.toUnderline(token.getKey()) + " != ?", token.getValue());
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE; // 和原来保持一致
    }

    @Override
    public WhereBuilder apply(WhereBuilder where, Token token) {
        return positiveOrNegate(where, token);
    }
}
